package com.sheep.classroompie.service;

import com.sheep.classroompie.entity.Course;
import com.sheep.classroompie.entity.Exam;
import com.sheep.classroompie.entity.Question;
import com.sheep.classroompie.entity.SubmitExam;
import com.sheep.classroompie.repository.ExamRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class ExamServiceImpl implements ExamService{
  @Autowired
  ExamRepository examRepository;

  @Override
  public Boolean addExam(Exam exam) {
    String publishMethod = exam.getPublishMethod();
    if("unified".equals(publishMethod)){
      System.out.println("统一发布");
      String numbers;
      ArrayList<Integer> arrayList = new ArrayList<>();
      for(int i = 0;i < 5;i++){
        int ran = (int) (1+ Math.random()*20);
        if(arrayList.contains(ran)){
          i--;
        }else{
          arrayList.add(ran);
        }
      }
      for(int i = 0;i < 2;i++){
        int ran = (int) (21+ Math.random()*5);
        if(arrayList.contains(ran)){
          i--;
        }else{
          arrayList.add(ran);
        }
      }
      numbers = arrayList.toString();
      exam.setProblems(numbers);
    }
    return examRepository.addExam(exam);
  }

  @Override
  public Question selectQuestionById(Integer questionId) {
    return examRepository.selectQuestionById(questionId);
  }

  @Override
  public List<Exam> selectExamsByCourseId(String courseId) {
    return examRepository.selectExamsByCourseId(courseId);
  }

  @Override
  public Exam selectExamById(Integer examId) {
    return examRepository.selectExamById(examId);
  }

  @Override
  public void submitExam(SubmitExam submitExam) {
    examRepository.submitExam(submitExam);
  }

  @Override
  public List<SubmitExam> selectSubmitExamsByCourseId(SubmitExam submitExam) {
    return examRepository.selectSubmitExamsByCourseId(submitExam);
  }

//  @Override
//  public SubmitExam selectExamById(SubmitExam submitExam) {
//    return examRepository.selectExamById(submitExam);
//  }
}
